/*
 * client.c: A very, very primitive HTTP client.
 * 
 * To run, try: 
 *      client www.cs.wisc.edu 80 /
 *
 * Sends one HTTP request to the specified HTTP server.
 * Prints out the HTTP response.
 *
 */

#include "rio.h"

/*
 * Send an HTTP request for the specified file 
 */
void clientSend(int fd, char *filename)
{
  char buf[MAXLINE];
  char hostname[MAXLINE];

  Gethostname(hostname, MAXLINE);

  /* Form and send the HTTP request */
  sprintf(buf, "GET %s HTTP/1.1\n", filename);
  //sprintf(buf, "GET [key] HTTP/1.1\n");
  sprintf(buf, "%shost: %s\n\r\n", buf, hostname);
  Rio_writen(fd, buf, strlen(buf));
}
  
/*
 * Read the HTTP response and print it out
 */
void clientPrint(int fd)
{
  rio_t rio;
  char buf[MAXBUF];  
  int length = 0;
  int n;
  
  Rio_readinitb(&rio, fd);

  /* Read and display the HTTP Header */
  n = Rio_readlineb(&rio, buf, MAXBUF);
  while (strcmp(buf, "\r\n") && (n > 0)) {
    printf("%s", buf);
    n = Rio_readlineb(&rio, buf, MAXBUF);	
    /* If you want to look for certain HTTP tags... */
    if (sscanf(buf, "Content-Length: %d ", &length) == 1) {
      printf("Content-Length : %d\n", length);
    }
  }

  //printf("Body:\n");
  /* Read and display the HTTP Body */
  n = Rio_readlineb(&rio, buf, MAXBUF);
  while (n > 0) {
    if(!strcmp(buf,"\r\n")){
      break;
    }
    printf("%s", buf);
    n = Rio_readlineb(&rio, buf, MAXBUF);
  }
}

int main(int argc, char *argv[])
{
  char *host, *filename;
  int port;
  int clientfd;

  char *servers_arg;
  char *key;
  char *value;
  char c;
  int i, n_tokens, is_write = 0;

  /*if (argc != 4) {
    fprintf(stderr, "Usage: %s <host> <port> <filename>\n", argv[0]);
    exit(1);
    }*/

  while ((c = getopt(argc, argv, "h:k:v:")) != -1) {
    switch(c) {
    case 'h':
      servers_arg = optarg;
      break;
    case 'k':
      key = optarg;
      break;
    case 'v':
      value = optarg;
      is_write = 1;
      break;
    }
  }

  char *s = strtok(servers_arg, ":,");
  char *tokens[100];
  i = 0;
  while (s != NULL) {
    tokens[i++] = s;
    s = strtok(NULL, ":,");
  }
  n_tokens = i;

  if (n_tokens % 2 == 1) {
    printf("Invalid server argument\n");
    exit(0);
  }

  int pos = 0;
  while (pos < n_tokens) {
    host = tokens[pos++];
    port = atoi(tokens[pos++]);

    if (is_write) {
      filename = malloc(strlen(key) + strlen(value) + 50);
      strcat(filename, "/[");
      strcat(filename, key);
      strcat(filename, "]=[");
      strcat(filename, value);
      strcat(filename, "]");
    }
    else {
      filename = malloc(strlen(key) + strlen(value) + 50);
      strcat(filename, "/[");
      strcat(filename, key);
      strcat(filename, "]");
    }

    //printf("Host : %s Port : %d Operation : %s\n", host, port, filename);

    /* Open a single connection to the specified host and port */
    printf("Trying to contact %s:%d\n", host, port);
    clientfd = Open_clientfd(host, port);

    if (clientfd < 0) {
      printf("Server %d not responding\n", pos/2);
      continue;
    }

    clientSend(clientfd, filename);
    clientPrint(clientfd);
    
    Close(clientfd);
    break;
  }
  
  exit(0);
}
